<?php
/**
 * CreateTime: 2023/8/29 11:41
 */

namespace App\Http\Controllers\ReportForms;

use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Consts\TableConsts\TableConsts;
use App\Func\ResponseFunc;
use App\Func\StaffFunc;
use App\Models\MeLawCase\MeLawCase;
use App\Models\MeLawCase\MeLawCaseBindMeDetails;
use App\Models\SMS\SMSLogModel;
use App\Scopes\MediationStaffScope;
use Illuminate\Support\Facades\DB;

class ReportFormsController
{
    public function getReportFormsByPhone(int $type){
        $lawCaseDetailModel = new MeLawCaseBindMeDetails();
        $query = $lawCaseDetailModel->select([
            DB::raw('owner_id as staff_id'),
            DB::raw('count(owner_id) as count')
        ])->where('owner_id','!=',null);
        switch ($type){
            default:
            case 1:
                $query = $query->where('created_time',">=",date("Y-m-d"));
                break;
            case 2:
                $query = $query->where('created_time',">=",date('Y-m-01'))
                    ->where('created_time',"<",date('Y-m-t'));
                break;
            case 3:
                $query = $query->where('created_time',">=", date('Y-01-01'))
                    ->where('created_time',"<", date('Y-12-31'));
                break;
        }
        $query = $query->groupBy('staff_id')->get()->toArray();
        if($query){
            $staffAll = StaffFunc::getStaffAllBySelect();
            $staffAll = array_column($staffAll,'staff_name','staff_id');
            foreach($query as $key=>$value){
                $query[$key]['staff_str'] = $staffAll[$value['staff_id']];
            }
        }
        return ResponseFunc::returnJson(
            'ok',
            $query,
            '获取外呼报表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function getReportFormBySMS(int $type){
        $lawCaseModel = new MeLawCase();
        $query = $lawCaseModel->select(['law_case_id']);
        switch ($type){
            default:
            case 1:
                $query = $query->where('created_at',">=",date("Y-m-d"));
                break;
            case 2:
                $query = $query->where('created_at',">=",date('Y-m-01'))
                    ->where('created_at',"<",date('Y-m-t'));
                break;
            case 3:
                $query = $query->where('created_at',">=", date('Y-01-01'))
                    ->where('created_at',"<", date('Y-12-31'));
                break;
        }
        $lawCaseIds = $query->get()->toArray();
        if($lawCaseIds){
            $lawCaseIds = array_column($lawCaseIds,'law_case_id');
        }

        DB::statement('CREATE TEMPORARY TABLE cy_temp_ids (fa_id INT, INDEX idx_fa_id (fa_id))');
        // 处理临时表中的数据
        $newArray = array_map(function($value) {
            return ['fa_id' => $value];
        }, $lawCaseIds);
        // 插入数据到临时表
        DB::table('temp_ids')->insert($newArray);
        // 获取短信发送情况
        $smsModel = new SMSLogModel();
        $smsCount = $smsModel
            ->withoutGlobalScope(MediationStaffScope::class)
            ->select('sms_log_id')
            ->join('temp_ids','fa_id','sms_log.law_case_id')
            ->count();
        $lawLetterSuccessCount = 0;
        $lawLetterFailCount = 0;
        if($smsCount>=0){
            $lawLetterSuccessCount = $smsModel
                ->select('sms_log_id')
                ->withoutGlobalScope(MediationStaffScope::class)
                ->where('send_law_latter_info','发送成功')
                ->join(TableConsts::ME_LAW_CASE,'law_case.law_case_id','sms_log.law_case_id')
                ->count();
            $lawLetterFailCount = $smsModel
                ->select('sms_log_id')
                ->withoutGlobalScope(MediationStaffScope::class)
                ->where('send_law_latter_info','!=','发送成功')
                ->join(TableConsts::ME_LAW_CASE,'law_case.law_case_id','sms_log.law_case_id')
                ->count();
        }
        return ResponseFunc::returnJson(
            'ok',
            [
                'law_letter_success_count' => $lawLetterSuccessCount,
                'law_letter_fail_count' => $lawLetterFailCount,
                'sms_count' => $smsCount,
            ],
            '获取短信报表成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );


    }


}